class Partition
{
public:
    ListNode* partition(ListNode* pHead, int x)
    {
        struct ListNode* ghead, * gtail, * lhead, * ltail;
        ghead = gtail = (ListNode*)malloc(sizeof(struct ListNode));
        lhead = ltail = (ListNode*)malloc(sizeof(struct ListNode));
        struct ListNode* cur = pHead;
        while (cur)
        {
            if (cur->val < x)
            {
                ltail = cur;
                lhead->next = cur;
            }
            else
            {
                gtail = cur;
                ghead->next = cur;
            }
            cur = cur->next;
        }
        ltail->next = ghead->next;
        gtail->next = NULL;
        struct ListNode* head = lhead->next;
        free(lhead);
        free(ghead);
        return head;
    }